package ua.soso.ui.dwr.error;

import org.aopalliance.aop.Advice;
import org.aspectj.lang.ProceedingJoinPoint;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ExceptionHandleAdvice implements Advice {
    private static final Logger LOG = LoggerFactory.getLogger(ExceptionHandleAdvice.class);

    public Object aroundDWRService(ProceedingJoinPoint jp) throws Throwable {
        long time = 0L;

        try {
            if (LOG.isDebugEnabled()) {
                time = System.currentTimeMillis();
                LOG.debug("Start " + jp.toLongString());
            }

            return jp.proceed(jp.getArgs());
        } catch (Exception e) {
            throw new DwrException("Unknown exception", e);
        } finally {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Finish " + jp.toLongString() + ": " + (System.currentTimeMillis() - time));
            }
        }
    }
}
